/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package l1j.server.server.model.item.action;

import l1j.server.Config;
import l1j.server.server.ClientThread;
import l1j.server.server.datatables.EnchantSuccessChance;
import l1j.server.server.datatables.EnchantSuccessChance.SuccessEnchantChance;
import l1j.server.server.datatables.LogEnchantTable;
import l1j.server.server.model.L1DeleteItem;
import l1j.server.server.model.L1PcInventory;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.identity.L1ItemId;
import l1j.server.server.serverpackets.S_OwnCharAttrDef;
import l1j.server.server.serverpackets.S_SPMR;
import l1j.server.server.serverpackets.S_ServerMessage;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1Armor;
import l1j.server.server.utils.Random;

public class Enchant {

	public static void scrollOfMagicdollEnchant(L1PcInstance pc,
			L1ItemInstance item, L1ItemInstance targetItem, ClientThread client) {
		if (targetItem.isMagicDoll()) {
			int currentLevel = targetItem.getEnchantLevel();
			if (currentLevel >= Config.DOLL_UPGRADE_MAX_LEVEL) {
				pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
			} else {
				L1PcInventory inv = pc.getInventory();
				if (inv.consumeItem(item.getItemId(), 1)) {
					int chance = Config.DOLL_UPGRADE_CHANCE;
					if (Random.chance100(chance)) {
						targetItem.setEnchantLevel(currentLevel + 1);
						pc.sendPackets(new S_ServerMessage(161, targetItem
								.getLogName(), "$245", "$247"));
						inv.updateItem(targetItem, L1PcInventory.COL_ENCHANTLVL);
						inv.saveItem(targetItem, L1PcInventory.COL_ENCHANTLVL);
					} else {
						pc.sendPackets(new S_ServerMessage(164, targetItem
								.getLogName(), "$245")); // \f1%0%s強烈的發出%1光芒就消失了。
						inv.deleteItem(targetItem);
						new L1DeleteItem()
								.logDeleteItem(pc, item, 1, "enchant");
					}
				}

			}

		} else {
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
		}
	}

	// 對武器施法的卷軸
	public static void scrollOfEnchantWeapon(L1PcInstance pc,
			L1ItemInstance l1iteminstance, L1ItemInstance l1iteminstance1,
			ClientThread client) {
		int itemId = l1iteminstance.getItem().getItemId();
		int safe_enchant = l1iteminstance1.getItem().get_safeenchant();
		int weaponId = l1iteminstance1.getItem().getItemId();
		if ((l1iteminstance1 == null) || (!l1iteminstance1.isWeapon())
				|| (safe_enchant < 0) || (l1iteminstance1.getBless() >= 128)) {
			pc.sendPackets(new S_ServerMessage(79));
			return;
		}
		if ((weaponId == 7) || (weaponId == 35) || (weaponId == 48)
				|| (weaponId == 73) || (weaponId == 105) || (weaponId == 120)
				|| (weaponId == 147) || (weaponId == 156) || (weaponId == 174)
				|| (weaponId == 175) || (weaponId == 224)) { // 象牙塔裝備
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
			return;
		}
		if ((weaponId >= 246) && (weaponId <= 249)) { // 試煉之劍
			if (itemId != L1ItemId.SCROLL_OF_ENCHANT_QUEST_WEAPON) { // 非試煉卷軸
				pc.sendPackets(new S_ServerMessage(79));
				return;
			}
		}
		if (itemId == L1ItemId.SCROLL_OF_ENCHANT_QUEST_WEAPON) { // 試煉卷軸
			if ((weaponId < 246) || (weaponId > 249)) { // 非試煉之劍
				pc.sendPackets(new S_ServerMessage(79));
				return;
			}
		}
		if ((weaponId == 36) || (weaponId == 183)
				|| ((weaponId >= 250) && (weaponId <= 255))) { // 幻象武器
			if (itemId != 40128) { // 非對武器施法的幻象卷軸
				pc.sendPackets(new S_ServerMessage(79));
				return;
			}
		}
		if (itemId == 40128) { // 對武器施法的幻象卷軸
			if ((weaponId != 36) && (weaponId != 183)
					&& ((weaponId < 250) || (weaponId > 255))) { // 非幻象武器
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}

		int enchant_level = l1iteminstance1.getEnchantLevel();

		if (itemId == L1ItemId.C_SCROLL_OF_ENCHANT_WEAPON) { // 受咀咒的 對武器施法的卷軸
			pc.getInventory().removeItem(l1iteminstance, 1);
			if (enchant_level < -6) {
				// -7以上失敗。
				FailureEnchant(pc, l1iteminstance1);
			} else {
				SuccessEnchant(pc, l1iteminstance1, client, -1);
			}
		} else if (enchant_level < safe_enchant) { // 強化等級小於安定值
			pc.getInventory().removeItem(l1iteminstance, 1);
			SuccessEnchant(pc, l1iteminstance1, client,
					RandomELevel(l1iteminstance1, itemId));
		} else if (enchant_level < 15) {
			pc.getInventory().removeItem(l1iteminstance, 1);
			if (safe_enchant == 0) {
				enchant_level = enchant_level + 6;
			}

			SuccessEnchantChance se = EnchantSuccessChance.getInstance()
					.getChance(enchant_level);
			if (Random.chance100(se.getWeapon_normal_chance() + pc.getUpgradeEnchantChance())) {
				int randomEnchantLevel = RandomELevel(l1iteminstance1, itemId);
				SuccessEnchant(pc, l1iteminstance1, client, randomEnchantLevel);
				// 強化成功『提升強化機率』歸０
				if (pc.getUpgradeEnchantChance() > 0) {
					try {
						pc.setUpgradeEnchantChance(0);
						pc.save();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					pc.sendPackets(new S_SystemMessage("失去了『荒神的祝福』。"));
				}
			} else if ((enchant_level >= 9)
					&& Random.chance100(se.getWeapon_normal_chance() * 2)) {
				// \f1%0%s 持續發出 產生激烈的 藍色的 光芒，但是沒有任何事情發生。
				pc.sendPackets(new S_ServerMessage(160, l1iteminstance1
						.getLogName(), "$245", "$248"));
			} else {
				FailureEnchant(pc, l1iteminstance1);
				// 強化失敗『提升強化機率』２％
				try {
					pc.setUpgradeEnchantChance(pc.getUpgradeEnchantChance() + 2);
					pc.save();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				pc.sendPackets(new S_SystemMessage("獲得『荒神的祝福』，下次強化機率提升: " 
						+ pc.getUpgradeEnchantChance() + "%"));
			}
		} else if (enchant_level >= 15) {
			pc.sendPackets(new S_SystemMessage("已經過於強大，無法再強化了。"));
		}
	}

	// 對盔甲施法的卷軸
	public static void scrollOfEnchantArmor(L1PcInstance pc,
			L1ItemInstance l1iteminstance, L1ItemInstance l1iteminstance1,
			ClientThread client) {
		int itemId = l1iteminstance.getItem().getItemId();
		int safe_enchant = ((L1Armor) l1iteminstance1.getItem())
				.get_safeenchant();
		int armorId = l1iteminstance1.getItem().getItemId();
		if ((l1iteminstance1 == null) || (!l1iteminstance1.isArmor())
				|| (safe_enchant < 0) || (l1iteminstance1.getBless() >= 128)) {
			pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
			return;
		}
		if (armorId == 20028 || armorId == 20082 || armorId == 20126
				|| armorId == 20173 || armorId == 20206 || armorId == 20232
				|| armorId == 21138 || armorId == 21051 || armorId == 21052
				|| armorId == 21053 || armorId == 21054 || armorId == 21055
				|| armorId == 21056 || armorId == 21140 || armorId == 21141) { // 象牙塔裝備
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
			return;
		}
		if ((armorId == 20161) || ((armorId >= 21035) && (armorId <= 21038))) { // 幻象裝備
			if (itemId != 40127) { // 非對盔甲施法的幻象卷軸
				pc.sendPackets(new S_ServerMessage(79));
				return;
			}
		}
		if (itemId == 40127) { // 對盔甲施法的幻象卷軸
			if ((armorId != 20161) && ((armorId < 21035) || (armorId > 21038))) { // 非幻象裝備
				pc.sendPackets(new S_ServerMessage(79)); // \f1何も起きませんでした。
				return;
			}
		}

		int enchant_level = l1iteminstance1.getEnchantLevel();
		if (itemId == L1ItemId.C_SCROLL_OF_ENCHANT_ARMOR) { // 受咀咒的 對盔甲施法的卷軸
			pc.getInventory().removeItem(l1iteminstance, 1);
			if (enchant_level < -6) {
				// -7以上失敗。
				FailureEnchant(pc, l1iteminstance1);
			} else {
				SuccessEnchant(pc, l1iteminstance1, client, -1);
			}
		} else if (enchant_level < safe_enchant) { // 強化等級小於安定值
			pc.getInventory().removeItem(l1iteminstance, 1);
			SuccessEnchant(pc, l1iteminstance1, client,
					RandomELevel(l1iteminstance1, itemId));
		} else if (enchant_level < 10) {
			pc.getInventory().removeItem(l1iteminstance, 1);
			if (safe_enchant == 0) {
				enchant_level = enchant_level + 4;
			}

			SuccessEnchantChance se = EnchantSuccessChance.getInstance()
					.getChance(enchant_level);
			if (Random.chance100(se.getArmor_normal_chance() + pc.getUpgradeEnchantChance())) {
				int randomEnchantLevel = RandomELevel(l1iteminstance1, itemId);
				SuccessEnchant(pc, l1iteminstance1, client, randomEnchantLevel);
				// 強化成功『提升強化機率』歸０
				if (pc.getUpgradeEnchantChance() > 0) {
					try {
						pc.setUpgradeEnchantChance(0);
						pc.save();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					pc.sendPackets(new S_SystemMessage("失去了『荒神的祝福』 。"));
				}
			} else if ((enchant_level >= 7)
					&& Random.chance100(se.getWeapon_normal_chance() * 2)) {
				// \f1%0%s 持續發出 產生激烈的 藍色的 光芒，但是沒有任何事情發生。
				pc.sendPackets(new S_ServerMessage(160, l1iteminstance1
						.getLogName(), "$245", "$248"));
			} else {
				FailureEnchant(pc, l1iteminstance1);
				// 強化失敗『提升強化機率』２％
				try {
					pc.setUpgradeEnchantChance(pc.getUpgradeEnchantChance() + 2);
					pc.save();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				pc.sendPackets(new S_SystemMessage("獲得『荒神的祝福』，下次強化機率提升: " 
						+ pc.getUpgradeEnchantChance() + "%"));
			}
		} else if (enchant_level >= 10) {
			pc.sendPackets(new S_SystemMessage("已經過於強大，無法再強化了。"));
		}
	}

	// 飾品強化卷軸
	public static void scrollOfEnchantAccessory(L1PcInstance pc,
			L1ItemInstance l1iteminstance, L1ItemInstance l1iteminstance1,
			ClientThread client, int type) {
		if ((l1iteminstance1 == null)
				|| (l1iteminstance1.getBless() >= 128)
				|| (!l1iteminstance1.isArmor()
						|| l1iteminstance1.getItem().getType() < 8
						|| l1iteminstance1.getItem().getType() > 12 || l1iteminstance1
						.getItem().getGrade() == -1)) { // 封印中
			pc.sendPackets(new S_ServerMessage(79));
			return;
		}
		int enchant_level = l1iteminstance1.getEnchantLevel();

		if (enchant_level < 0 || enchant_level >= 5) { // 強化上限 + 10
			pc.sendPackets(new S_ServerMessage(79));
			return;
		}
		// 已經強化了武卷或防卷其中一種 不能強化其他種
		if (type < 2 && (type + 1) != l1iteminstance1.getAttrEnchantKind() && l1iteminstance1.getAttrEnchantKind() > 0) {
			pc.sendPackets(new S_ServerMessage(79));
			return;
		}

		SuccessEnchantChance se = EnchantSuccessChance.getInstance().getChance(
				enchant_level);
		if (Random.chance100(se.getAccessory_normal_chance())) {
			switch (type) {
			case 0:
				// 攻擊力+1 在飾品上借用attrkind欄位 1 = 武卷
				l1iteminstance1.setAttrEnchantKind(1);
				pc.getInventory().updateItem(l1iteminstance1,
						L1PcInventory.COL_ATTR_ENCHANT_KIND);
				pc.getInventory().saveItem(l1iteminstance1,
						L1PcInventory.COL_ATTR_ENCHANT_KIND);
				break;
			case 1:
				// 防禦力AC-1 在飾品上借用attrkind欄位 2 = 防卷
				l1iteminstance1.setAttrEnchantKind(2);
				pc.getInventory().updateItem(l1iteminstance1,
						L1PcInventory.COL_ATTR_ENCHANT_KIND);
				pc.getInventory().saveItem(l1iteminstance1,
						L1PcInventory.COL_ATTR_ENCHANT_KIND);
				break;
			case 2:
				// 其它
				break;
			default:
				pc.sendPackets(new S_ServerMessage(79));
				return;
			}
			int randomEnchantLevel = RandomELevel(l1iteminstance1,
					l1iteminstance1.getId());
			SuccessEnchant(pc, l1iteminstance1, client, randomEnchantLevel);
		} else {
			FailureEnchant(pc, l1iteminstance1);
		}
	}

	public static void scrollOfEnchantWeaponAttr(L1PcInstance pc,
			L1ItemInstance l1iteminstance, L1ItemInstance l1iteminstance1,
			ClientThread client) {
		int itemId = l1iteminstance.getItem().getItemId();
		if ((l1iteminstance1 == null) || (!l1iteminstance1.isWeapon())
				|| (l1iteminstance1.getBless() >= 128)) {
			pc.sendPackets(new S_ServerMessage(79));
			return;
		}
		if (l1iteminstance1.getItem().get_safeenchant() < 0) { // 強化不可
			pc.sendPackets(new S_ServerMessage(1453)); // 此裝備無法使用強化。
			return;
		}
		if (l1iteminstance1.isEquipped()) {
			pc.sendPackets(new S_SystemMessage("必須脫掉武器才能夠進行強化。"));
			return;
		}
		
		int newAttrEnchantKind = 0;
		int newAttrEnchantLevel = 0;
		if (itemId == 41429) { // 風の武器強化スクロール
			newAttrEnchantKind = 8;
		} else if (itemId == 41430) { // 地の武器強化スクロール
			newAttrEnchantKind = 1;
		} else if (itemId == 41431) { // 水の武器強化スクロール
			newAttrEnchantKind = 4;
		} else if (itemId == 41432) { // 火の武器強化スクロール
			newAttrEnchantKind = 2;
		}
		if (l1iteminstance1.getAttrEnchantKind() == 0) {
			newAttrEnchantLevel = 1;
		} else {
			newAttrEnchantLevel = l1iteminstance1.getAttrEnchantLevel();
		}
		l1iteminstance1.setAttrEnchantKind(newAttrEnchantKind);
		pc.getInventory().updateItem(l1iteminstance1,
				L1PcInventory.COL_ATTR_ENCHANT_KIND);
		pc.getInventory().saveItem(l1iteminstance1,
				L1PcInventory.COL_ATTR_ENCHANT_KIND);
		l1iteminstance1.setAttrEnchantLevel(newAttrEnchantLevel);
		pc.getInventory().updateItem(l1iteminstance1,
				L1PcInventory.COL_ATTR_ENCHANT_LEVEL);
		pc.getInventory().saveItem(l1iteminstance1,
				L1PcInventory.COL_ATTR_ENCHANT_LEVEL);
		pc.getInventory().removeItem(l1iteminstance, 1);
	}
	
	public static void stoneOfEnchantWeaponAttr(L1PcInstance pc,
			L1ItemInstance l1iteminstance, L1ItemInstance l1iteminstance1,
			ClientThread client) {
		int itemId = l1iteminstance.getItem().getItemId();
		int newAttrEnchantKind = l1iteminstance1.getAttrEnchantKind();
		int newAttrEnchantLevel = itemId - 41433;
		if ((l1iteminstance1 == null) || (!l1iteminstance1.isWeapon())
				|| (l1iteminstance1.getBless() >= 128)) {
			pc.sendPackets(new S_ServerMessage(79));
			return;
		}
		if (l1iteminstance1.getItem().get_safeenchant() < 0) { // 強化不可
			pc.sendPackets(new S_ServerMessage(1453)); // 此裝備無法使用強化。
			return;
		}
		if (l1iteminstance1.getAttrEnchantKind() == 0
				|| l1iteminstance1.getAttrEnchantLevel() > newAttrEnchantLevel) {
			pc.sendPackets(new S_ServerMessage(79));
			return;
		}
		if (l1iteminstance1.isEquipped()) {
			pc.sendPackets(new S_SystemMessage("必須脫掉武器才能夠進行強化。"));
			return;
		}
		if (l1iteminstance1.getAttrEnchantLevel() == newAttrEnchantLevel) {
			if (Random.chance100(50)) {
				newAttrEnchantLevel = newAttrEnchantLevel + 1;
				pc.sendPackets(new S_SystemMessage("『恭喜』屬性階位卷軸強化成功。"));
			} else {
				newAttrEnchantLevel = 1;
				pc.sendPackets(new S_SystemMessage("『殘念』屬性階位卷軸強化失敗。"));
			}
		} else {
//			if (itemId != 41434 && itemId != 41435) {
//				pc.getItem(itemId - 1, 1, 0, "");
//			}
			pc.sendPackets(new S_SystemMessage("『恭喜』屬性階位卷軸強化成功。"));
		}
		l1iteminstance1.setAttrEnchantKind(newAttrEnchantKind);
		pc.getInventory().updateItem(l1iteminstance1,
				L1PcInventory.COL_ATTR_ENCHANT_KIND);
		pc.getInventory().saveItem(l1iteminstance1,
				L1PcInventory.COL_ATTR_ENCHANT_KIND);
		l1iteminstance1.setAttrEnchantLevel(newAttrEnchantLevel);
		pc.getInventory().updateItem(l1iteminstance1,
				L1PcInventory.COL_ATTR_ENCHANT_LEVEL);
		pc.getInventory().saveItem(l1iteminstance1,
				L1PcInventory.COL_ATTR_ENCHANT_LEVEL);
		pc.getInventory().removeItem(l1iteminstance, 1);
	}
	
	public static void brandRune(L1PcInstance pc,
			L1ItemInstance l1iteminstance, L1ItemInstance l1iteminstance1,
			ClientThread client) {
		if (l1iteminstance1 == null
				|| (!l1iteminstance1.isWeapon() && !l1iteminstance1.isArmor())) {
			pc.sendPackets(new S_ServerMessage(79));
			return;
		}
		if (l1iteminstance1.isEquipped()) {
			pc.sendPackets(new S_SystemMessage("必須脫掉裝備才能夠烙印。"));
			return;
		}
		int runeId = l1iteminstance.getItemId() - 41443;
		if (l1iteminstance1.getArmorRuneOne() == 0) {
			l1iteminstance1.setArmorRuneOne(runeId);
			pc.getInventory().updateItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_ONE);
			pc.getInventory().saveItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_ONE);
			pc.getInventory().removeItem(l1iteminstance, 1);
		} else if (l1iteminstance1.getArmorRuneTwo() == 0) {
			l1iteminstance1.setArmorRuneTwo(runeId);
			pc.getInventory().updateItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_TWO);
			pc.getInventory().saveItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_TWO);
			pc.getInventory().removeItem(l1iteminstance, 1);
		} else if (l1iteminstance1.getArmorRuneThree() == 0) {
			l1iteminstance1.setArmorRuneThree(runeId);
			pc.getInventory().updateItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_THREE);
			pc.getInventory().saveItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_THREE);
			pc.getInventory().removeItem(l1iteminstance, 1);
		} else {
			l1iteminstance1.setArmorRuneOne(runeId);
			pc.getInventory().updateItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_ONE);
			pc.getInventory().saveItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_ONE);
			l1iteminstance1.setArmorRuneTwo(0);
			pc.getInventory().updateItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_TWO);
			pc.getInventory().saveItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_TWO);
			l1iteminstance1.setArmorRuneThree(0);
			pc.getInventory().updateItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_THREE);
			pc.getInventory().saveItem(l1iteminstance1,
					L1PcInventory.COL_ARMOR_RUNE_THREE);
			pc.getInventory().removeItem(l1iteminstance, 1);
		}
	}

	// 象牙塔對武器施法的卷軸
	public static void scrollOfEnchantWeaponIvoryTower(L1PcInstance pc,
			L1ItemInstance l1iteminstance, L1ItemInstance l1iteminstance1,
			ClientThread client) {
		int weaponId = l1iteminstance1.getItem().getItemId();
		if ((l1iteminstance1 == null) || (!l1iteminstance1.isWeapon())) {
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
			return;
		}
		if (l1iteminstance1.getBless() >= 128) { // 封印中強化不可
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
			return;
		}
		if (weaponId != 7 && weaponId != 35 && weaponId != 48 && weaponId != 73
				&& weaponId != 105 && weaponId != 120 && weaponId != 147
				&& weaponId != 156 && weaponId != 174 && weaponId != 175
				&& weaponId != 224) { // 非象牙塔裝備
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
			return;
		}
		int safe_enchant = l1iteminstance1.getItem().get_safeenchant();
		if (l1iteminstance1.getEnchantLevel() < safe_enchant) {
			pc.getInventory().removeItem(l1iteminstance, 1);
			SuccessEnchant(pc, l1iteminstance1, client, 1);
		} else {
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
		}
	}

	// 象牙塔對盔甲施法的卷軸
	public static void scrollOfEnchantArmorIvoryTower(L1PcInstance pc,
			L1ItemInstance l1iteminstance, L1ItemInstance l1iteminstance1,
			ClientThread client) {
		int armorId = l1iteminstance1.getItem().getItemId();
		if ((l1iteminstance1 == null) || (!l1iteminstance1.isArmor())
				|| (l1iteminstance1.getBless() >= 128)) {
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
			return;
		}
		if (armorId != 20028 && armorId != 20082 && armorId != 20126
				&& armorId != 20173 && armorId != 20206 && armorId != 20232
				&& armorId != 21138 && armorId != 21051 && armorId != 21052
				&& armorId != 21053 && armorId != 21054 && armorId != 21055
				&& armorId != 21056 && armorId != 21140 && armorId != 21141) { // 非象牙塔、泡水裝備
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
			return;
		}
		int safe_enchant = l1iteminstance1.getItem().get_safeenchant();
		if (l1iteminstance1.getEnchantLevel() < safe_enchant) {
			pc.getInventory().removeItem(l1iteminstance, 1);
			SuccessEnchant(pc, l1iteminstance1, client, 1);
		} else {
			pc.sendPackets(new S_ServerMessage(79)); // \f1沒有任何事情發生。
		}
	}

	// 強化成功
	private static void SuccessEnchant(L1PcInstance pc, L1ItemInstance item,
			ClientThread client, int i) {
		int itemType2 = item.getItem().getType2();

		String[][] sa = { { "", "", "", "", "" },
				{ "$246", "", "$245", "$245", "$245" },
				{ "$246", "", "$252", "$252", "$252" } };
		String[][] sb = { { "", "", "", "", "" },
				{ "$247", "", "$247", "$248", "$248" },
				{ "$247", "", "$247", "$248", "$248" } };
		String sa_temp = sa[itemType2][i + 1];
		String sb_temp = sb[itemType2][i + 1];

		pc.sendPackets(new S_ServerMessage(161, item.getLogName(), sa_temp,
				sb_temp));
		int oldEnchantLvl = item.getEnchantLevel();
		int newEnchantLvl = oldEnchantLvl + i;
		int safe_enchant = item.getItem().get_safeenchant();
		item.setEnchantLevel(newEnchantLvl);
		client.getActiveChar().getInventory()
				.updateItem(item, L1PcInventory.COL_ENCHANTLVL);

		if (newEnchantLvl > safe_enchant) {
			client.getActiveChar().getInventory()
					.saveItem(item, L1PcInventory.COL_ENCHANTLVL);
		}
		if ((item.isWeapon()) && (Config.LOGGING_WEAPON_ENCHANT != 0)) {
			if ((safe_enchant == 0)
					|| (newEnchantLvl >= Config.LOGGING_WEAPON_ENCHANT)) {
				LogEnchantTable logenchant = new LogEnchantTable();
				logenchant.storeLogEnchant(pc.getId(), item.getId(),
						oldEnchantLvl, newEnchantLvl);
			}
		} else if (item.isArmor() && (Config.LOGGING_ARMOR_ENCHANT != 0)) {
			if ((safe_enchant == 0)
					|| (newEnchantLvl >= Config.LOGGING_ARMOR_ENCHANT)) {
				LogEnchantTable logenchant = new LogEnchantTable();
				logenchant.storeLogEnchant(pc.getId(), item.getId(),
						oldEnchantLvl, newEnchantLvl);
			}
		}

		if (item.isArmor()) { // 防具類
			if (item.isEquipped()) {
				if ((item.getItem().getType() < 8 || item.getItem().getType() > 12)) {
					pc.addAc(-i);
				}
				int armorId = item.getItem().getItemId();
				// 強化等級+1，魔防+1
				int[] i1 = { 20011, 20110, 21123, 21124, 21125, 21126, 120011 };
				// 抗魔法頭盔、抗魔法鏈甲、林德拜爾的XX、受祝福的 抗魔法頭盔
				for (int j = 0; j < i1.length; j++) {
					if (armorId == i1[j]) {
						pc.addMr(i);
						pc.sendPackets(new S_SPMR(pc));
						break;
					}
				}
				// 強化等級+1，魔防+2
				int[] i2 = { 20056, 120056, 220056 };
				// 抗魔法斗篷
				for (int j = 0; j < i2.length; j++) {
					if (armorId == i2[j]) {
						pc.addMr(i * 2);
						pc.sendPackets(new S_SPMR(pc));
						break;
					}
				}
			}
			pc.sendPackets(new S_OwnCharAttrDef(pc));
		}
	}

	// 強化失敗
	private static void FailureEnchant(L1PcInstance pc, L1ItemInstance item) {
		String[] sa = { "", "$245", "$252" }; // ""、藍色的、銀色的
		int itemType2 = item.getItem().getType2();

		if (item.getEnchantLevel() < 0) { // 強化等級為負值
			sa[itemType2] = "$246"; // 黑色的
		}
		pc.sendPackets(new S_ServerMessage(164, item.getLogName(),
				sa[itemType2])); // \f1%0%s 強烈的發出%1光芒就消失了。
		pc.getInventory().removeItem(item, item.getCount());
		new L1DeleteItem().logDeleteItem(pc, item, 1, "enchant");
	}

	// 隨機強化等級
	private static int RandomELevel(L1ItemInstance item, int itemId) {
		int value = 0;
		if ((itemId == L1ItemId.B_SCROLL_OF_ENCHANT_ARMOR)
				|| (itemId == L1ItemId.B_SCROLL_OF_ENCHANT_WEAPON)
				|| (itemId == 140129) || (itemId == 140130)) {
			SuccessEnchantChance se = EnchantSuccessChance.getInstance()
					.getChance(item.getEnchantLevel());
			if (item.getEnchantLevel() < -1) {
				return 1;
			}
			if (item.getItem().getType2() == 1) {
				while (value == 0) {
					if (Random.chance100(se.getWeapon_bless_1_chance())) {
						value = 1;
					} else if (Random.chance100(se.getWeapon_bless_2_chance())) {
						value = 2;
					} else if (Random.chance100(se.getWeapon_bless_3_chance())) {
						value = 3;
					}
				}
			} else if (item.getItem().getType2() == 2
					&& item.getItem().getType() <= 7) {
				while (value == 0) {
					if (Random.chance100(se.getArmor_bless_1_chance())) {
						value = 1;
					} else if (Random.chance100(se.getArmor_bless_2_chance())) {
						value = 2;
					} else if (Random.chance100(se.getArmor_bless_3_chance())) {
						value = 3;
					}
				}
			} else if (item.getItem().getType2() == 2
					&& item.getItem().getType() > 7) {
				while (value == 0) {
					if (Random.chance100(se.getAccessory_bless_1_chance())) {
						value = 1;
					} else if (Random.chance100(se
							.getAccessory_bless_2_chance())) {
						value = 2;
					} else if (Random.chance100(se
							.getAccessory_bless_3_chance())) {
						value = 3;
					}
				}
			}
			return value;
		}
		return 1;
	}
}
